#include "stdafx.h"
#include "PreciseTime.h"


CPreciseTime::CPreciseTime()
: m_counter(0)
{
	m_frequency = XApi::QueryPerformanceFrequency();
}

CPreciseTime CPreciseTime::now()
{
	CPreciseTime time;
	time.m_counter = XApi::QueryPerformanceCounter();
	time.m_frequency = XApi::QueryPerformanceFrequency();
	return time;
}

CPreciseTime& CPreciseTime::operator+= (const CPreciseTime& rhs)
{
	assert(m_frequency == rhs.m_frequency);
	m_counter += rhs.m_counter;
	return *this;
}

CPreciseTime& CPreciseTime::operator-= (const CPreciseTime& rhs)
{
	assert(m_frequency == rhs.m_frequency);
	assert(m_counter > rhs.m_counter);	//	negative time values not supported
	m_counter -= rhs.m_counter;
	return *this;
}

CPreciseTime& CPreciseTime::operator*=(float rhs)
{
	float result = static_cast<float>(m_counter) * rhs;
	m_counter = static_cast<uint64>(result);
	return *this;
}

CPreciseTime CPreciseTime::operator+ (const CPreciseTime& rhs) const
{
	CPreciseTime result(*this);
	result += rhs;
	return result;
}

CPreciseTime CPreciseTime::operator- (const CPreciseTime& rhs) const
{
	CPreciseTime result(*this);
	result -= rhs;
	return result;
}

CPreciseTime CPreciseTime::operator* (float rhs) const
{
	CPreciseTime result(*this);
	result *= rhs;
	return result;
}

CPreciseTime::operator float () const
{
	return static_cast<float>(m_counter)/m_frequency;
}